﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using TimeTableTool.DTO;

namespace TimeTableTool
{
    class ReacheableTableDao 
    {
        SQLiteConnection connection;

        public ReacheableTableDao(SQLiteConnection connection)
        {
            this.connection = connection;
        }


        public List<TimeTableDTO> GetTimeTables(SQLiteCommand cmnd)
        {
            using (cmnd)
            {
                using (SQLiteDataReader reader = cmnd.ExecuteReader())
                {
                    List<TimeTableDTO> timeTableList = new List<TimeTableDTO>();
                    while (reader.Read())
                    {
                        TimeTableDTO timeTable = new TimeTableDTO();
                        timeTable.Id = int.Parse(reader["id"].ToString());
                        timeTable.LineId = int.Parse(reader["line_id"].ToString());
                        timeTable.StationId = int.Parse(reader["station_id"].ToString());
                        timeTable.DayType = reader["day_type"].ToString();
                        timeTable.Time = int.Parse(reader["time"].ToString());
                        timeTableList.Add(timeTable);
                    }
                    return timeTableList;
                }
            }
        }

        public void BulkInsert(List<ReacheableTableDTO> reacheableTables)
        {
            SQLiteCommand insertCmnd = new SQLiteCommand(connection);
            insertCmnd.CommandText = "INSERT INTO reacheable_table (line_id, start_station_id, destination_station_id, distance) "+
                                     "VALUES (@lineIdValue, @startStationId,@destinationStationId, @distance);";

            SQLiteTransaction transaction = connection.BeginTransaction();
            foreach (ReacheableTableDTO reacheableTable in reacheableTables)
            {
                insertCmnd.Parameters.AddWithValue("lineIdValue", reacheableTable.LineId);
                insertCmnd.Parameters.AddWithValue("startStationId", reacheableTable.StartStationId);
                insertCmnd.Parameters.AddWithValue("destinationStationId", reacheableTable.DestinationStationId);
                insertCmnd.Parameters.AddWithValue("distance", reacheableTable.Distance);
                insertCmnd.ExecuteNonQuery();
            }
            transaction.Commit();

           
        }

    }
}
